<?php
class Clogin extends CI_Controller{

	function login()
		{
			$this->load->model('mlogin', '', TRUE);
		}

	function index(){
		if ($this->session->userdata('login') == TRUE)
		{
			redirect('homez');
		}
		else
		{
			$this->load->view('vlogin');
		}
	}

	function process_login(){
		$this->load->model('mlogin');
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
		$this->form_validation->set_rules('user', 'Username', 'required');
		$this->form_validation->set_rules('password', 'Password', 'required');		
		
		if ($this->form_validation->run() == TRUE)
		{
			$user = $this->input->post('user');
			$password = $this->input->post('password');
			
			if ($this->mlogin->cek_user($user, $password) == TRUE)
			{
				$queryx = $this->mlogin->get_id($user);
					foreach ($queryx as $row){
					$id = $row->id_pemandu;
					}
				$query = $this->mlogin->siapa($user);
					foreach ($query as $row)
					{
						$id_pemandu = $row->id_pemandu;
						$tipe_user = $row->tipe_user;
					}
				$data = array('id_pemandu' => $id_pemandu, 'user' => $user, 'login' => TRUE, 'tipe_user' => $tipe_user,
							'login_date' => date("l, d-M-Y"), 'login_time' => date("h:i:s A"));	
				$this->session->set_userdata($data);
				redirect('homez');
			}else{
				$this->session->set_flashdata('message', 'Maaf, user dan/atau password Anda salah');
				redirect('clogin/index');
			}
		}
		else
		{
			$this->load->view('vlogin');
		}
	}
	
	function process_logout()
	{
		$this->session->sess_destroy();
		redirect('homez', 'refresh');
	}

}

?>
